package com.panopset.app;

import static com.panopset.Util.*;

import java.awt.FileDialog;
import java.io.File;
import java.util.Properties;
import java.util.Vector;

import com.panopset.AbstractApplication;
import com.panopset.Alert;
import com.panopset.Util;
import com.panopset.gui.container.PanField;

/**
 * Manage application properties/defaults.
 *
 * @author Karl Dinwiddie
 *
 */
public class DocumentManager {

    private final String defaultFileName;
    private Vector<PanField<?>> containers;
    private final Properties properties = new Properties();
    private static DocumentManager documentManager;

    /**
     * @return Current active DocumentManager
     */
    public static DocumentManager asDocumentManager() {
        if (documentManager == null) {
            documentManager = new DocumentManager();
        }
        return documentManager;
    }

    private static void setDocumentManager(DocumentManager documentManager) {
        DocumentManager.documentManager = documentManager;
    }

    public DocumentManager() {
        this(PERIOD + DocumentManager.class.getCanonicalName());
    }

    public DocumentManager(String defaultFileName) {
        this.defaultFileName = defaultFileName;
        setDocumentManager(this);
    }

    public DocumentManager(Class<?> clazz) {
        this(PERIOD + clazz.getCanonicalName());
    }

    public Vector<PanField<?>> getContainers() {
        if (containers == null) {
            containers = new Vector<PanField<?>>();
        }
        return containers;
    }

    public void load() {
        loadProperties(properties, getFile());
        for (PanField<?> container : getContainers()) {
            container.load(properties);
        }
        for (PanField<?> container : getContainers()) {
            container.doneLoading();
        }
    }

    public void save() {
        Alert.yellow(x("Saving..."));
        for (PanField<?> container : getContainers()) {
            container.flush(properties);
        }
        saveProperties(properties, getFile());
    }

    public Properties getProperties() {
        return properties;
    }

    public void doNew(AbstractApplication app) {
        File newFile = Util.getFileFromUser(app.getFlexFrame().getFrame(),
                getFile(), x("New file"), FileDialog.SAVE);
        if (newFile != null) {
            setFile(newFile);
            app.getFlexFrame().getFrame().setTitle(
                    app.getApplicationName() + " ~ " + newFile.getName());
            save();
        }
    }

    public void doOpen(AbstractApplication app) {
        File newFile = Util.getFileFromUser(app.getFlexFrame().getFrame(),
                getFile(), x("Open file"), FileDialog.LOAD);
        if (newFile != null) {
            setFile(newFile);
            app.getFlexFrame().getFrame().setTitle(
                    app.getApplicationName() + " ~ " + newFile.getName());
            load();
        }
    }

    public void doSave(AbstractApplication app) {
        save();
    }

    public void doSaveAs(AbstractApplication app) {
        doNew(app);
        save();
    }

    private File file;

    private File getFile() {
        if (file == null) {
            file = new File(defaultFileName);
        }
        return file;
    }

    public void setFile(File newFile) {
        file = newFile;
    }

    public String get(String key) {
        return getProperties().getProperty(key);
    }

    public void put(String key, String value) {
        getProperties().put(key, value);
    }
}
